/*
 
 * 
 * user : will
 * 
 * */

mui.plusReady(function() {
	function creatDom() {
		return item = {
			item: document.createElement('div'), //大div
			avatar: document.createElement('div'), //头像div
			avta_img: document.createElement('img'), // 头像
			contetn: document.createElement('div'), //信息div
			item_p: document.createElement('p') //信息内容
		}
	}
	
	function setitem(img,content,uid){
		creatDom()
						item.avatar.setAttribute('class', 'msg_avatar')
						item.avta_img.setAttribute('src', img)
						item.avatar.appendChild(item.avta_img)
						item.item.appendChild(item.avatar)
						if(parseInt(uid) == selfid) {
							item.item.setAttribute('class', 'msg_item_right msg_item')
						} else {
							item.item.setAttribute('class', 'msg_item_left msg_item')
						}
						item.contetn.setAttribute('class', 'msg_content')
						item.item_p.innerText = content
						item.contetn.appendChild(item.item_p)
						item.item.appendChild(item.contetn)
						parDom.appendChild(item.item)
						backBottom()
	}
//	$('#scroll').scroll({indicators: false});



	var scroll = mui('.mui-scroll-wrapper').scroll();
	var parDom = document.querySelector('#msg-list')
	var parview = plus.webview.currentWebview();
	var activityId = parview.activityid
	var selfid, selfimg, selfname
	mui.ajax('http://' + ip + '/xiaojv/Api/index.php?m=chat&p=front&a=selfData', {
		data: {

		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			selfimg = data.avatar
			selfid = data.id
			selfname = data.username
//			console.log(selfid)
			getchat()
		},
		error: function(xhr, type, errorThrown) {
			console.log(type)
		}
	});

	document.querySelector('.backup').addEventListener('tap', function() {
		mui.back()
	})
	
	
	function backBottom(){
		scroll.reLayout();
		scroll.scrollToBottom(100);
	}
	function getchat(){
		mui.ajax('http://' + ip + '/xiaojv/Api/index.php?m=chat&p=front&a=selectchat', {
			data: {
				avid: activityId
			},
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			success: function(data) {
				var a = data.length
				if(a > 0){
					for(b = a-1; b >= 0; b--) {
					var ite = data[b]
						setitem(ite.img,ite.converContent,ite.id)
					}
					backBottom()
				}
				
				
			},
			error: function(xhr, type, errorThrown) {
				console.log(type)
			}
		});
	}
	

	mui('.mui-scroll-wrapper').scroll({
		deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
	});

	var ws = new WebSocket('ws://'+ip+':9090')
	ws.onopen = function() {
		console.log('连接成功')
	}


	function send() {

		var msg_box = document.getElementById("msg-text");
		var content = msg_box.value;
		if(content != '' && content != null && content != undefined) {
			var reg = new RegExp("\r\n", "g");
			content = content.replace(reg, "");
			var msg = {
				'content': content.trim(),
				'type': 'user',
				'uid': selfid,
				'acid': activityId,
				'uimg': selfimg
			};
			sendMsg(msg);
			msg_box.value = '';
			// todo 清除换行符
		} else {
			mui.toast('发送内容为空')
		}

	}
	ws.onmessage = function(e) {
		//					console.log(e)
		//				
		var msg = JSON.parse(e.data)
		if(msg.acid == activityId) {
			if(msg.type == 'user') {
				setitem(msg.uimg,msg.content,msg.uid)
			}
		}
		backBottom()
	}

	function sendMsg(msg) {
		var data = JSON.stringify(msg);
		ws.send(data);
	}

	document.querySelector('.send').addEventListener('tap', function() {
		send();
		backBottom()
	})

})